package com.sskj.givecoin.p.fragment;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.givecoin.m.HttpConfig;
import com.sskj.givecoin.m.bean.FabiDetailListBean;
import com.sskj.givecoin.m.bean.FabiListBean;
import com.sskj.givecoin.m.bean.HaiPageBean;
import com.sskj.givecoin.m.bean.HttpData;
import com.sskj.givecoin.m.http.JsonCallBack;
import com.sskj.givecoin.m.http.JsonConverter;
import com.sskj.givecoin.p.base.BasePresenter;
import com.sskj.givecoin.utils.MD5Util;
import com.sskj.givecoin.utils.ToastUtil;
import com.sskj.givecoin.v.fragment.FabiBuyAndSellFragment;

import java.util.List;

import io.reactivex.Flowable;

public class FabiBuyPresenter extends BasePresenter<FabiBuyAndSellFragment> {
    public Flowable<List<FabiListBean>> getFabiList(int page, int size, boolean isBuy) {
        return OkGo.<HttpData<HaiPageBean<FabiListBean>>>get(HttpConfig.BASE_URL + HttpConfig.FABI_LIST)
                .params("type", isBuy ? "pmma" : "sell")
                .params("p", page)
                .params("size", size)
                .converter(new JsonConverter<HttpData<HaiPageBean<FabiListBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(haiPageBeanHttpData -> haiPageBeanHttpData.getData().getRes());
    }

    public void order(String tpwd, String order_no, String total_num, String total_price, boolean isBuy) {
        OkGo.<HttpData<FabiDetailListBean>>post(HttpConfig.BASE_URL + HttpConfig.CREATE_FABI_ORDER)
                .params("tpwd", MD5Util.md5FiveTime(tpwd))
                .params("order_no", order_no)
                .params("total_num", total_num)
                .params("total_price", total_price)
                .params("type", isBuy ? 2 : 1)
                .execute(new JsonCallBack<HttpData<FabiDetailListBean>>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData<FabiDetailListBean>> response) {
                        if (response.body().getStatus()==200){
                            mView.checkOk(response.body().getData().getOrder_num(),isBuy);
                        }
                        ToastUtil.showShort(response.body().getMsg());
                    }
                });
    }

//    /**
//     * 验证支付密码
//     *
//     * @param pass
//     */
//    public void checkPayPwd(String pass) {
//        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.CHECK_PAY_PASS)
//                .params("mobile", MyAppLication.getMobile())
//                .params("tpwd", pass)
//                .execute(new JsonCallBack<HttpData>(this) {
//                    @Override
//                    public void _onSuccess(Response<HttpData> response) {
//                        HttpData httpListData = response.body();
////                        mView.checkOk();
//                        if (httpListData.getStatus() == 200) {
//                            mView.checkOk();
//                        } else {
//                            ToastUtil.showShort(httpListData.getMsg());
//                        }
//                    }
//                });
//    }
}
